# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Jesse Luehrs <archlinux@tozt.net>

pkgname=rbw
pkgver=1.8.3
pkgrel=1
pkgdesc='Unofficial Bitwarden CLI'
arch=('loong64' 'x86_64')
url='https://git.tozt.net/rbw'
license=('MIT')
depends=('glibc' 'gcc-libs' 'pinentry')
makedepends=('rust')
options=('!lto')
source=("$url/snapshot/${pkgname}-${pkgver}.tar.gz")
sha512sums=('57f7890c552b6c489614239a6c14d7e9a3a5c4407848c62c0fa13748d4776964cf174969cf3ec796c0e313f505c4f94466c9ca242bd453e655c17a6145ed4a2d')
b2sums=('30ed59974a4b78f71d3a5859dec275b9b597e6da505782c8c042f5e0e844741a072c7885015316e5ea6040081c263fabd5848d3a9a8fe0c21fe264011e4ba1b3')

prepare() {
  cd "$pkgname-$pkgver"
  cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
}

build() {
  cd "$pkgname-$pkgver"
  cargo build --frozen --release --all-features

  # generate shell auto-completions
  for completion in bash fish zsh; do
    cargo run --frozen --release --bin rbw -- \
      gen-completions "$completion" > "$completion-completions"
  done
}

check() {
  cd "$pkgname-$pkgver"
  cargo test --frozen --all-features
}

package() {
  cd "$pkgname-$pkgver"

  # binaries
  install -vDm755 -t "$pkgdir/usr/bin" target/release/rbw{,-agent}

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md CHANGELOG.md

  # shell auto-completions
  install -vDm644 bash-completions "$pkgdir/usr/share/bash-completion/completions/rbw"
  install -vDm644 fish-completions "$pkgdir/usr/share/fish/vendor_completions.d/rbw.fish"
  install -vDm644 zsh-completions "$pkgdir/usr/share/zsh/site-functions/_rbw"
}
